﻿using AdverseEventReportSystem.BLL.Scheduling;
using AdverseEventReportSystem.BLL.SchedulingSystem;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Scheduling.Controllers
{
    /// <summary>
    /// 排班周期
    /// </summary>
    public class ScheduleCycleController : BaseController
    {
        // GET: Scheduling/ScheduleCycle
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Edit()
        {
            ScheduleCycle scheduleCycle = new ScheduleCycle();
            int id = WebUtils.GetInt("id");
            if (id > 0)
            {
                //编辑 取数据赋值
                OperateResult result = ScheduleCycleManager.GetScheduleCycle(id);
                if (result.Result == 1)
                {
                    scheduleCycle = (ScheduleCycle)result.Body;
                }
            }
            return View(scheduleCycle);
        }

        /// <summary>
        /// 保存排班周期
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save()
        {
            ScheduleCycle scheduleCycle = new ScheduleCycle();
            scheduleCycle.Enabled = DataConvert.StrToInt(WebUtils.GetString("hdnEnabled"));
            scheduleCycle.Id = DataConvert.StrToInt(WebUtils.GetString("hdnId"), 0);
            scheduleCycle.Sort = DataConvert.StrToInt(Request["txtSchedulSort"], 1);
            scheduleCycle.TypeName = WebUtils.GetString("txtSchedulName");
            OperateResult resultInfo = ScheduleCycleManager.SaveScheduleCycle(scheduleCycle);
            return JsonDate(resultInfo);
        }
        /// <summary>
        /// 列表加载
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pagesize", 30);

            OperateResult resultInfo = ScheduleCycleManager.GetScheduleCycleList(pageIndex, pageSize);
            string NoData = "{Rows:[],Total:0}";
            if (resultInfo.Result == 1)
            {
                var listData = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                return JsonDate(listData);
            }
            else
            {
                NoData = "{\"Total\": 0,\"Rows\":[],\"Result\":\"" + resultInfo.Result + "\",\"Body\":\"" + resultInfo.Body + "\"}";
            }
            return Content(NoData);
        }

        /// <summary>
        /// 状态变化
        /// </summary>
        /// <returns></returns>
        public ActionResult Enabled()
        {
            int id = WebUtils.GetInt("Id", 0);
            OperateResult resultInfo = new OperateResult();
            if (id <= 0) { resultInfo.Result = 0; resultInfo.Body = "ID必须大于0"; return JsonDate(resultInfo); }
            resultInfo = ScheduleCycleManager.GetScheduleCycle(id);
            if (resultInfo.Result != 1) return JsonDate(resultInfo);
            ScheduleCycle scheduleCycle = (ScheduleCycle)resultInfo.Body;
            scheduleCycle.Enabled = scheduleCycle.Enabled == 0 ? 1 : 0;
            resultInfo = ScheduleCycleManager.SaveScheduleCycle(scheduleCycle);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public ActionResult Delete()
        {
            int id = WebUtils.GetInt("Id", 0);
            OperateResult resultInfo = new OperateResult();
            if (id <= 0) { resultInfo.Result = 0; resultInfo.Body = "ID必须大于0"; return JsonDate(resultInfo); }
            resultInfo = ScheduleCycleManager.GetScheduleCycle(id);
            if (resultInfo.Result != 1) return JsonDate(resultInfo);
            ScheduleCycle scheduleCycle = (ScheduleCycle)resultInfo.Body;
            resultInfo = ScheduleCycleManager.DelScheduleCycle(scheduleCycle.Id);
            return JsonDate(resultInfo);
        }
    }
}